package mygame;

import java.awt.Point;

import javax.swing.ImageIcon;

import main.Board;
import player.Bullit;
import player.Gun;
import player.Reticle;
import player.Shot;
import player.Triangle;

import sideScrollerGame.Player;

import quadtree.DoublePoint;

import utilPackage.Star;

public class SpaceDeathBoard extends Board {

	public Player player, player2;
	public Triangle square1, square2;
	private Triangle square3;
	private Triangle square4;
	private Triangle square5;
	private Triangle square6;
	private Triangle square7;
	private Triangle square8;
	private Triangle square9;
	private Triangle square10;
	private Gun testGun;
	private Reticle testReticle;

	SpaceDeathBoard(boolean debug) {
		super();
		super.setDebug(debug);
		gameInit();
		setCursorImage("");
	}

	public void gameInit(){
		/*square1 = new Triangle(275, 375, 50, 50, 2);
		square1.setGameTree(gameTree);
		square2 = new Triangle(275, 425, 50, 50, -2);
		square2.setGameTree(gameTree);
*/
		square3 = new Triangle(150, 500, 50, 50, 0.2);
		square3.setGameTree(gameTree);
		square4 = new Triangle(450, 500, 50, 50, -0.2);
		square4.setGameTree(gameTree);
		gameTree.insertSprite(new DoublePoint(150, 500), square3);
		gameTree.insertSprite(new DoublePoint(450, 500), square4);
		/*square5 = new Triangle(150, 600, 50, 50, 2);
		square5.setGameTree(gameTree);
		square6 = new Triangle(450, 600, 50, 50, -2);
		square6.setGameTree(gameTree);
		gameTree.insertSprite(new DoublePoint(150, 600), square5);
		gameTree.insertSprite(new DoublePoint(450, 600), square6);
		square7 = new Triangle(150, 700, 50, 50, 2);
		square7.setGameTree(gameTree);
		square8 = new Triangle(450, 700, 50, 50, -2);
		square8.setGameTree(gameTree);
		gameTree.insertSprite(new DoublePoint(150, 700), square7);
		gameTree.insertSprite(new DoublePoint(450, 700), square8);
		square9 = new Triangle(150, 200, 50, 50, 2);
		square9.setGameTree(gameTree);
		square10 = new Triangle(450, 200, 50, 50, -2);
		square10.setGameTree(gameTree);*/

		testGun = new Gun();
		testReticle = new Reticle();
		gameTree.insertSprite(new DoublePoint(testReticle.getX(), testReticle.getY()), testReticle);
		gameTree.insertSprite(new DoublePoint(testGun.getX(), testGun.getY()), testGun);
		/*square1 = null;
		square2 = null;
		square3 = null;
		square4 = null;
		square5 = null;
		square6 = null;
		square7 = null;
		square8 = null;
		square9 = null;
		square10 = null;*/
	}


	/* To add a key handler,  you must also modify the TAdapter 
	 * and InputHandler classes
	 */
	public void gameHandleInput(){
		if(userInput.inputHandler.DOWN_PUSH == true){
			player.keyPressed("VK_DOWN");

		}
		if(userInput.inputHandler.UP_PUSH == true){
			player.keyPressed("VK_UP");

		}
		if(userInput.inputHandler.RIGHT_PUSH == true){
			player.keyPressed("VK_RIGHT");

		}	
		if(userInput.inputHandler.LEFT_PUSH == true){
			player.keyPressed("VK_LEFT");	
		}
		if(userInput.inputHandler.S_PUSH == true){
			player2.keyPressed("VK_DOWN");

		}
		if(userInput.inputHandler.W_PUSH == true){
			player2.keyPressed("VK_UP");

		}
		if(userInput.inputHandler.D_PUSH == true){
			player2.keyPressed("VK_RIGHT");

		}	
		if(userInput.inputHandler.A_PUSH == true){
			player2.keyPressed("VK_LEFT");	
		}
		if(userInput.inputHandler.SPACE_PUSH == true){
			player.keyPressed("VK_SPACE");
			Shot s = new Shot(player.getX(),player.getY());
			gameTree.insertSprite(new DoublePoint(player.getX(), player.getY() ),s  );	
		}
		if(userInput.inputHandler.SHIFT_PUSH == true){
			player.keyPressed("VK_SHIFT");

		}
		if(userInput.inputHandler.DOWN_RELEASE == true){
			player.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;

		}
		if(userInput.inputHandler.UP_RELEASE == true){
			player.keyReleased("VK_UP");
			userInput.inputHandler.UP_RELEASE = false;
		}
		if(userInput.inputHandler.RIGHT_RELEASE== true){
			player.keyReleased("VK_RIGHT");
			userInput.inputHandler.RIGHT_RELEASE = false;
		}	
		if(userInput.inputHandler.LEFT_RELEASE == true){
			player.keyReleased("VK_LEFT");	
			userInput.inputHandler.LEFT_RELEASE = false;
		}
		if(userInput.inputHandler.DOWN_RELEASE == true){
			player.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;

		}
		if(userInput.inputHandler.W_RELEASE == true){
			player2.keyReleased("VK_UP");
			userInput.inputHandler.W_RELEASE = false;
		}
		if(userInput.inputHandler.D_RELEASE== true){
			player2.keyReleased("VK_RIGHT");
			userInput.inputHandler.D_RELEASE = false;
		}	
		if(userInput.inputHandler.A_RELEASE == true){
			player2.keyReleased("VK_LEFT");	
			userInput.inputHandler.A_RELEASE = false;
		}
		if(userInput.inputHandler.S_RELEASE == true){
			player2.keyReleased("VK_DOWN");
			userInput.inputHandler.S_RELEASE = false;

		}
		if(userInput.inputHandler.SPACE_RELEASE == true){
			player.keyReleased("VK_SPACE");

		}
		if(userInput.inputHandler.SHIFT_RELEASE == true){
			player.keyReleased("VK_SHIFT");
		}
		if(userInput.inputHandler.MOUSE_MOVED == true) {
			userInput.inputHandler.MOUSE_MOVED = false;
			testReticle.setX(userInput.MouseX);
			testReticle.setY(userInput.MouseY);
			mouseMoved(userInput.MouseX, userInput.MouseY);
		}
		if(userInput.inputHandler.MOUSE_PRESSED == true) {
			userInput.inputHandler.MOUSE_PRESSED = false;
			if(userInput.inputHandler.MOUSE_LEFT_CLICK) {
				userInput.inputHandler.MOUSE_LEFT_CLICK = false;
				mousePressed(userInput.MouseX, userInput.MouseY);
				testReticle.setWidth(5);
			}
			else if(userInput.inputHandler.MOUSE_RIGHT_CLICK) {
				userInput.inputHandler.MOUSE_RIGHT_CLICK = false;
				Triangle tmp = new Triangle(userInput.MouseX, userInput.MouseY, 50, 50, -2);
				tmp.setGameTree(gameTree);
				gameTree.insertSprite(new DoublePoint(userInput.MouseX, userInput.MouseY), 
						tmp);
				tmp = null;
			}
		}
	}

	public void generateBackground(){
		for (int i=0; i < 5; i++) {
			for (int j=0; j < 6; j++) {
				int x = (int)(Math.random()*BOARD_WIDTH);
				int y = (int)( Math.random()*BOARD_HEIGTH);
				Star star = new Star(x,y);
				ImageIcon ii = new ImageIcon(this.getClass().getResource(star.starpix));
				star.setImage(ii.getImage());
				gameTree.insertSprite(new DoublePoint(x,y),star);
			}
		}        
	}

	public void handleInput(){
		try{
			gameHandleInput();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void mousePressed(int X, int Y) {
		//super.mouseClicked(e);
		// TODO Auto-generated method stub
		int offset = 75;
		int x = X-offset;
		int y = Y-offset;
	
		Bullit tmp = new Bullit(testGun.getXChange() + testGun.getX(), testGun.getYChange() + testGun.getY(), 
				testGun.getXDirection() * (int) (5 * Math.cos(testGun.getRadians())), (int) (5 * Math.sin(testGun.getRadians())));
		gameTree.insertSprite(new DoublePoint(testGun.getX(), testGun.getY()), 
				tmp);
		tmp = null;
		//MouseClickQueryBox
		/*Vector<PointNodeElement<Sprite>> checkElements;
		checkElements = gameTree.spritesTree.queryObject(new Point(x, y), 
				new Dimension(offset*2,offset*2));
		for(int i = 0; i < checkElements.size(); i++) {
			try {
				Triangle spriter = (Triangle)checkElements.get(i).getElement();
				if(spriter.pointWithin(x+offset, y+offset)) {
					((Triangle)spriter).die();
				}
			} catch (Exception exc) {
				//do nothing
			}
		}*/
	}
	
	public void mouseMoved(int X, int Y) {
		// TODO Auto-generated method stub
		testGun.setEndX(X);
		testGun.setEndY(Y);
	}
}
